package cn.tedu.property_management_system.service.Impl;

import cn.tedu.property_management_system.entity.Administrator;
import cn.tedu.property_management_system.entity.Building;
import cn.tedu.property_management_system.entity.VO.AdministratorVO;
import cn.tedu.property_management_system.entity.dto.AdministratorDTO;
import cn.tedu.property_management_system.entity.dto.AdministratorDTV;
import cn.tedu.property_management_system.ex.ServiceException;
import cn.tedu.property_management_system.mapper.AdministratorMapper;
import cn.tedu.property_management_system.service.AdministratorService;
import cn.tedu.property_management_system.utils.JsonPage;
import cn.tedu.property_management_system.utils.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class AdministratorServiceImpl implements AdministratorService {

    @Autowired
    private AdministratorMapper administratorMapper;


    @Override
    public int add(AdministratorDTO administratorDTO) {
        Administrator administrator = new Administrator();
        log.debug("@" + administratorDTO.getUser());
        log.debug("@" + administratorMapper.getUserId(administratorDTO.getUser()));
        log.debug("");
        log.debug("");
        administrator.setUserId(administratorMapper.getUserId(administratorDTO.getUser()));

        administrator.setRoleId(administratorMapper.getRoleId(administratorDTO.getAdministrators()));

        administrator.setGmtCreate(LocalDateTime.now());

        administrator.setGmtModified(LocalDateTime.now());

        int row = administratorMapper.insert(administrator);

        if (row == 0) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "插入失败");
        }
        return row;
    }

    @Override
    public int delete(Integer id) {
        int row = administratorMapper.delete(id);
        if (row == 0) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "插入失败");
        }
        return row;
    }

    @Override
    public int deletes(List<AdministratorVO> administratorVOList) {
        List<Integer> list = new ArrayList<>();
        for (AdministratorVO administratorVO : administratorVOList) {
            administratorMapper.delete(administratorVO.getId());
        }

        return 1;
    }


    @Override
    public List<AdministratorVO> getAdministrator() {
        return administratorMapper.select();
    }

    @Override
    public List<String> getUserNameList() {
        return administratorMapper.userNameList();
    }

    @Override
    public List<String> getAdminTypeList() {
        return administratorMapper.adminTypeList();
    }

    @Override
    public int updateById(AdministratorDTV administratorDTV) {
        


        Administrator administrator = administratorMapper.getAdministrator(administratorDTV.getId());

        String password = administratorMapper.getPasswordById(administrator.getUserId());

        if (!(password.equals(administratorDTV.getPassword1()) &&administratorDTV.getPassword2().equals(administratorDTV.getPassword3()))) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "修改密码失败");
        }

        int row = administratorMapper.updatePassword(administrator.getUserId(), administratorDTV.getPassword3());
        if (row == 0) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "服务器忙........");
        }
        return row;
    }


    public JsonPage<AdministratorVO> pageSelect(Integer pageSize, Integer pageIndex){
        PageHelper.startPage(pageSize,pageIndex);
        List<AdministratorVO> list=administratorMapper.select();
        return JsonPage.restPage(new PageInfo<>(list));
    }



}
